package com.gollum.system.domain.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.gollum.common.utils.StringUtils;
import com.gollum.jpa.domain.HertherBaseEnity;
import lombok.*;
import lombok.experimental.Accessors;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * 用户实体
 *
 * @author Herther
 * @version 1.0.0
 * @ClassName SysUser.java
 * @createTime 2022年08月07日 14:32:00
 */

@Getter
@Setter
@ToString
@Accessors(chain=true)
@Entity
@Table(name = "sys_user")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value={"hibernateLazyInitializer", "roles"})
public class SysUser extends HertherBaseEnity {

    private static final long serialVersionUID = 1L;

    /** 用户账号 */
    @Column(name = "username",length = 50)
    private String username;

    /** 用户昵称 */
    @Column(name = "nick_name",length = 50)
    private String nickName;

    /** 用户邮箱 */
    @Column(name = "email",length = 50)
    private String email;

    /** 手机号码 */
    @Column(name = "mobile",length = 20)
    private String mobile;

    /** 用户性别 0=男,1=女,2=未知 */
    @Column(name = "sex",length = 10)
    private String sex;

    /** 用户头像 */
    @Column(name = "avatar")
    private String avatar;

    /** 密码 */
    @Column(name = "password",length = 100)
    private String password;

    /** 帐号状态（0正常 1停用） */
    @Column(name = "status", length = 10)
    private String status;

    /** 最后登录IP */
    @Column(name = "login_ip", length = 50)
    private String loginIp;

    /** 最后登录时间 */
    @Column(name = "login_date")
    @DateTimeFormat(
            pattern = "yyyy-MM-dd HH:mm:ss"
    )
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date loginDate;

    /** 生日 **/
    @DateTimeFormat(
            pattern = "yyyy-MM-dd HH:mm:ss"
    )
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "birthday")
    private Date birthday;

    /** 用户类型 **/
    @Column(name = "user_type",length = 50)
    private String userType;

    /** 真实姓名 **/
    @Column(name = "real_name",length = 30)
    private String realName;

    /** 角色 **/
    @OneToMany(fetch = FetchType.LAZY)
    @JoinTable(
        name = "sys_user_role",
        joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
        inverseJoinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")}
    )
    private List<SysRole> roles;


    /** 岗位表 **/
    @OneToMany(fetch = FetchType.LAZY)
    @JoinTable(
        name = "sys_user_post",
        joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "post_id", referencedColumnName = "id")
    )
    private List<SysPost> sysPosts;



    /** 是否是超级管理员 0 非超管 1 超管 **/
    @Column(name = "has_admin", length = 10)
    private String hasAdmin;

    /** 部门ID **/
    @Column(name = "deptId", length = 255)
    private Long deptId;

    /**
     * 是否为管理员
     *
     * @auther: Herther
     * @version 1.0.0
     * @date: 2022/8/26 23:53
     */
    public Boolean isAdmin(){
        return StringUtils.isEmpty(getHasAdmin()) || !StringUtils.equals(getHasAdmin(),"1") ? false : true;
    }

}
